// pages/add/add.js
const date = new Date()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabTitle: ["支出","收入"],
    selectedTab: 0,
    selectDate: false, //控制date组件的显示
    valueError: false, //控制确认按钮，在没有值得时候弹出报错信息
    day: date.getDate(), //获取当前日期
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    moneyValue: '', //输入的金额数
    icon: '',
    iconBgColor: '',
    iconName: ''
  },
  // 点击日期
  handleSelectDate(){
    console.log('date')
    this.setData({
      selectDate: true
    })
  },
  //取消日期
  handleCancel(){
    this.setData({
      selectDate: false
    })
  },
  //确定日期
  handleSure(event){
    const day = event.detail.day
    const year = event.detail.year
    const month = event.detail.month
    this.setData({
      selectDate: false,
      day,
      year,
      month
    })
  },
  // 点击计算器按钮
  handleClick(event){
    console.log(event)
    const moneyValue = event.detail.tempValue
    this.setData({
      moneyValue
    })
  },
  //点击计算器按钮：确定
  handleCalSure(event){
    const moneyValue = event.detail.moneyValue

    const addPageTemp = app.globalData.addPage //相当于arr

    const day= this.data.day
    const year = this.data.year
    const month= this.data.month
    const accountDate = `${month}月${day}日`
    const id = new Date(`${this.data.year}-${this.data.month}-${this.data.day}`).getTime() //时间戳

    console.log(moneyValue)
    if(moneyValue){
      wx.switchTab({
        url: '/pages/bill/bill',
      })
      //保存add页面所有的数据
      let addData = {
        id: id,
        day: day,
        year: year,
        month: month,
        accountDate: accountDate,
        moneyValue: this.data.moneyValue,
        icon: this.data.icon,
        iconBgcolor: this.data.iconBgcolor,
        iconName: this.data.iconName,
        selectedTab: this.data.selectedTab
      }
      //修改全局变量中的addPage
      app.globalData.addPage.push(addData)
      console.log('addData',addData)
    }else {
      console.log('数据有误')
      this.setData({
        valueError: true
      })
      setTimeout(()=> {
        this.setData({
          valueError: false
        })
      },2000)
    }
  },
  //获取支出项的默认icon
  defaultPayIcon(event){
    this.setData({
      icon: event.detail.selectedPayIcon,
      iconName: event.detail.selectedPayName,
      iconBgcolor: event.detail.selectedPayBgcolor,
      selectedTab: event.detail.selectedPayTab
    })
  },
  //获取收入的默认icon
  defaultIncomeIcon(event){
    console.log(event.detail)
    this.setData({
      icon: event.detail.selectedIncomeIcon,
      iconName: event.detail.selectedIncomeName,
      iconBgcolor: event.detail.selectedIncomeBgcolor,
      selectedTab: event.detail.selectedIncomeTab
    })
  },
  //获取支出项的信息
  handlePayIcon(event){
    console.log(event.detail)
    this.setData({
      icon: event.detail.selectedPayIcon,
      iconName: event.detail.selectedPayName,
      iconBgcolor: event.detail.selectedPayBgcolor,
      selectedTab: event.detail.selectedTab
    })
  },
  //获取收入项的信息
  handleIncomeIcon(event){
    console.log(event.detail)
    this.setData({
      icon: event.detail.selectedIncomeIcon,
      iconName: event.detail.selectedIncomeName,
      iconBgcolor: event.detail.selectedIncomeBgcolor,
      selectedTab: event.detail.selectedTab
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})